﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace CamHKVision
{
    [Serializable]
    public class CamHKVisionSetting
    {
        public string m_strName;
        public string DeviceIpAddress
        {
            get;
            set;
        }
        
       
        public bool MirrorRow
        {
            get;
            set;
        }
        public bool MirrorCol
        {
            get;
            set;
        }
        public bool NeedRotate
        {
            get;
            set;
        }
        public double dRotateAngle
        {
            get;
            set;
        }
        public bool CropImage
        {
            get;
            set;
        }
        public int ImageWidth
        {
            get;
            set;
        }
        public int ImageHeight
        {
            get;
            set;
        }
        public double ExporsureMs
        {
            get;
            set;
        }

        public int Gain
        {
            get;
            set;
        }
        public bool UseSampleImage
        {
            get;
            set;
        }
        public CamHKVisionSetting()
        {
           
            ExporsureMs = 10.0;
            DeviceIpAddress = "192.168.0.12";
            Gain = 0;
        }

        public bool TragMode
        {
            get;
            set;
        }
        public static CamHKVisionSetting Load(string strName)
        {
            CamHKVisionSetting pDoc;
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(CamHKVisionSetting));
            FileStream fsReader = null;
            try
            {
                fsReader = File.OpenRead(Application.StartupPath + @"/MuduleParameter/CamHKVisionSetting" + "_" + strName + ".xml");
                pDoc = (CamHKVisionSetting)xmlSerializer.Deserialize(fsReader);
                pDoc.m_strName = strName;
                fsReader.Close();
            }
            catch (IOException)
            {
                if (fsReader != null)
                {
                    fsReader.Close();
                }
                pDoc = new CamHKVisionSetting();
                pDoc.m_strName = strName;
            }
            catch (Exception)
            {
                if (fsReader != null)
                {
                    fsReader.Close();
                }
                pDoc = new CamHKVisionSetting();
                pDoc.m_strName = strName;

            }
            return pDoc;
        }
        public bool Save()
        {
            if (!Directory.Exists(Application.StartupPath + @"/MuduleParameter/"))
            {
                Directory.CreateDirectory(Application.StartupPath + @"/MuduleParameter/");
            }
            FileStream fsWriter = new FileStream(Application.StartupPath + @"/MuduleParameter/CamHKVisionSetting" + "_" + m_strName + ".xml", FileMode.Create, FileAccess.Write, FileShare.Read);
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(CamHKVisionSetting));
            xmlSerializer.Serialize(fsWriter, this);
            fsWriter.Flush(true);
            fsWriter.Close();

            return true;
        }
    }
}
